﻿/****************************************************************************
 * Tutorial: CDCase Test Class: Testing Array, Object, Dictionary
 * File: CDCase.as
 * 
 * Description: This is a tutorial designed to show how to create
 * 				and use the Dictionary Class and differences between
 * 				Arrays , Objects and Dictionaries
 *
 * @author:  Haelix @ Zombie Flambe 
 * @site:    http://www.zombieflambe.com
 * @email:   haelix@zombieflambe.com
 * @version: 1.0
 * @created: 1st January 2009
 ****************************************************************************/

package zombieflambe {
	import flash.utils.Dictionary;
	import flash.display.Sprite;
	
	public class CDCase extends Sprite {
		
		private var caseArr:Array;
		private var caseObj:Object;
		private var caseDict:Dictionary;
		private const MAX_CDS:int = 100000;
		private const MY_CD_TITLE:String = "cdTitle_90000";
		
		public function CDCase() {
			caseArr  = new Array();
			caseObj  = new Object();

			/**********************************
			 * public function Dictionary(weakKeys:Boolean = false)
			 * weakKeys:Boolean (default = false) — Instructs the Dictionary object to use "weak" references 
			 *   on object keys. If the only reference to an object is in the specified Dictionary object, 
			 *   the key is eligible for garbage collection and is removed from the table when the object 
			 *   is collected. 
			 *****************************/
			caseDict = new Dictionary( true );
			
			var myCD = new Object();
			myCD.cdTitle = MY_CD_TITLE;
			
			// Initialize all our cases
			var i:int = 0;
			for( i ; i < MAX_CDS; i++ )  {
				var tmpCD:Object = new Object();
				tmpCD.cdTitle = "cdTitle_" + i;
				
				caseArr.push( tmpCD );
				
				// Objects need a string-based index
				caseObj[ tmpCD.cdTitle ] = tmpCD;
				
				// The "key" is the actual object.. our CD
				//   the 'value' is a string that's just the cd's title.
				caseDict[ tmpCD ] = tmpCD.cdTitle;
				
				// This lets us hold onto the reference of the tmp cd
				if( tmpCD.cdTitle == myCD.cdTitle )  
					myCD = tmpCD;
				
			}
						
			var result1:int 		    = searchArray( myCD );
			var result2:int	 		    = searchObject( myCD );
			var result3:Boolean 		= searchDictionary( myCD );
			trace( "Result1: " + result1 );
			trace( "Result2: " + result2 );
			trace( "Result3: " + result3 );
			
			clean();
		}
		
		private function searchArray( myCD:Object ):int  {
			var i:int =  0; //MAX_CDS - 1;
			var titleFoundAt:int = -1;
			for( i; i < MAX_CDS; i++ )  {
				if( caseArr[ i ] == myCD )  {
					titleFoundAt = i;
					break;
				}
			}
			return titleFoundAt;
		}
		
		private function searchObject( myCD:Object ):int  {
			var i:int = MAX_CDS - 1;
			var titleFoundAt:int = -1;
			for( i; i >= 0; i-- )  {
				if( caseObj[ "cdTitle_" + i  ] == myCD )  { 
					titleFoundAt = i;
					break;
				}
			}
			return titleFoundAt;
		}
		
		private function searchDictionary( myCD:Object ):Boolean  {
			var titleFound:Boolean = false;
			if( caseDict[ myCD ] )
				titleFound = true;
			
			return titleFound;
		}
		
		
		public function clean():void  {
			var i:int = MAX_CDS - 1;
			while( i >= 0  )  {
				caseArr.splice( i , 1 );
				delete caseDict[ "cdTitle_" + i ];
				delete caseObj[ "CD_" + i ];
				i--;
			}
			caseArr = null;
		}
	}
}